<?php
namespace AuctionCore\CarResource;

/**
 * 车辆维修保养报告Model
 * Class CarRepairInfo
 * @package AuctionCore\CarResource
 */
class CarRepairInfo {

	private $rid = null;

	/**
	 * 构造
	 * @param  int $car_id  车辆id
	 * @return array
	 */
	public function __construct($rid=0) {
		$this->rid = $rid;
	}

	/**
	 * 通过ID或vin
	 * 获取检测报告
	 */
	public function get_repair_info($option){
		if(is_numeric($option)){
			$result = M('car_repair_info')->where(" rid='".$option."'")->find();
		}else{
			$result = M('car_repair_info')->where("vin='".$option."'")->find();
		}
		if(empty($result)){
			return false;
		}
		$result_data['rid'] = $result['rid'];
		$result_data['introduction'] = array(
				'vin' => $result['vin'],
				'third_party'=>$result['third_party'],
				'createtime' => $result['createtime'],
				'componentanalyzerepairtimes'=>$result['componentanalyzerepairtimes'],
				'outsideanalyzerepair'=>$result['outsideanalyzerepair'],
				'constructanalyzerepairrecords'=>$result['constructanalyzerepairrecords'],
				'lastmaintaintime'=>$result['lastmaintaintime'],
				'lastrepairtime'=>$result['lastrepairtime'],
				'maintaintimes'=>$result['maintaintimes'],
				'mileageeveryyear'=>$result['mileageeveryyear'],
				'transmissiontype'=>'',
				'manufacturer'=>'',
				'makedate'=>'',
				'cartype'=>'',
				'displacement'=>'',
				'productionarea'=>'',
				'effluentstandard'=>'',
		);

		$detail_data= unserialize($result['normalrepairrecords']);
		if($result['third_party']==1){
			$result_data['normalrepairrecords'] =  $detail_data['normalRepairRecords'];
			$result_data['introduction']['transmissiontype'] = $detail_data['transmissionType'];
			$result_data['introduction']['manufacturer_1'] = $detail_data['manufacturer'];
			$result_data['introduction']['manufacturer'] = $detail_data['manufacturer'].' '.$detail_data['modelName'].' '.$detail_data['displacement'].' '.$detail_data['transmissionType'];
			$result_data['introduction']['makedate'] = $detail_data['makeDate'];
			$result_data['introduction']['cartype'] = $detail_data['carType'];
			$result_data['introduction']['displacement'] = $detail_data['displacement'];
			$result_data['introduction']['productionarea'] = $detail_data['productionArea'];
			$result_data['introduction']['effluentstandard'] = $detail_data['effluentStandard'];
		}else{
			$result_data['normalrepairrecords'] = $detail_data['normalRepairRecords'];
			$result_data['introduction']['displacement'] = $detail_data['basic']['displacement'];
			$result_data['introduction']['manufacturer']  = $detail_data['basic']['brand'].$detail_data['basic']['model'].' '.$detail_data['basic']['displacement'].' '.$detail_data['basic']['gearbox'];
			$result_data['introduction']['manufacturer_1']  = $detail_data['basic']['brand'];
			$result_data['introduction']['makedate']    =  $detail_data['basic']['year'];
		}
		$result_data['third_party'] = $result['third_party'];
		return $result_data;
	}

	/**
	 * @param $data
	 * @return mixed
	 */
	public function inset_data($data){
		$state = M('car_repair_info')->add($data);
		return $state;
	}

	/**根据vin码获取报告
	 * @param $vin
	 * @return mixed
	 */
	public function getInfoByVin($vin){
		$state = M('car_repair_info')->field('rid')->where("vin='".$vin."'")->find();
		return $state;
	}

	/**修改报告
	 * @param $vin
	 * @param $data
	 * @return bool
	 */
	public function updateInfo($id,$data){
		$state = M('car_repair_info')->where("rid=".$id)->save($data);
		return $state;
	}
    /**根据vin码查询car_id
     * @param $vin
     * @param $data
     * @return array
     */
    public function get_cars($vin){
        $cars = M('cars');
        $data = $cars->where("vin='".$vin."'")->find();
        return $data;
    }
    /**根据rid和dealer_id查询用户最新查询同一条维保的时间
     * @param $rid
     * @param $data
     * @return createtime
     */
    public function get_end_createtime($dealer_id){
        $map['rid'] = array('eq',$this->rid);
        $map['dealer_id'] = array('eq',$dealer_id);
        $repair_info = M('dealer_repair_log')->where($map)->order('createtime desc')->find();
        return $repair_info['createtime'];
    }
	public function checkRepairLog($rid,$dealer_id){
		$map['rid'] = array('eq',$rid);
		$map['dealer_id'] = array('eq',$dealer_id);
		return M('dealer_repair_log')->where($map)->find();
	}
}
